package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private FacilityInfoService facilityInfoService;
    @Autowired
    private LabelInfoService labelInfoService;
    @Autowired
    private AttrValueService attrValueService;
    @Autowired
    private PaymentTypeService paymentTypeService;
    @Autowired
    private LeaseTermService leaseTermService;


    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;

    @Override
    public IPage<RoomItemVo> getRoomItemVoIPage(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.getRoomItemVoIPage(page,queryVo);
    }

    @Override
    public RoomDetailVo getRoomDetailVoById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        RoomInfo roomInfo = super.getById(id);
        BeanUtils.copyProperties(roomInfo, roomDetailVo);

        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        roomDetailVo.setApartmentInfo(apartmentInfo);

//        todo 第一种写法（没写sql）
//        List<GraphInfo> graphInfoList = graphInfoService.list(new LambdaQueryWrapper<GraphInfo>()
//                .eq(GraphInfo::getItemType, ItemType.ROOM)
//                .eq(GraphInfo::getItemId, id));

//        todo 1.流式操作
//        List<GraphVo> graphVoList = graphInfoList.stream().map(graphInfo -> {
//            GraphVo graphVo = new GraphVo();
//            BeanUtils.copyProperties(graphInfo, graphVo);
//            return graphVo;
//        }).collect(Collectors.toList());

//        todo 2.forEach
//        ArrayList<GraphVo> graphVoList = new ArrayList<>();
//        graphInfoList.forEach(graphInfo -> {
//            GraphVo graphVo = new GraphVo();
//            BeanUtils.copyProperties(graphInfo,graphVo);
//            graphVoList.add(graphVo);
//        });
//        roomDetailVo.setGraphVoList(graphVoList);

        List<GraphVo> graphVoList = graphInfoService.getGraphVoListByRoomId(id);
        List<AttrValueVo> attrValueVoList = attrValueService.getAttrValueVoListByRoomId(id);
        List<FacilityInfo> facilityInfoList = facilityInfoService.getFacilityInfoListByRoomId(id);
        List<LabelInfo> labelInfoList = labelInfoService.getLabelInfoListByRoomId(id);
        List<PaymentType> paymentTypeList = paymentTypeService.getPaymentTypeListByRoomId(id);
        List<LeaseTerm> leaseTermList = leaseTermService.getLeaseTermListByRoomId(id);


        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);

        return roomDetailVo;
    }

    @Override
    public void saveOrUpdateRoomSubmitVo(RoomSubmitVo roomSubmitVo) {
        Long id = roomSubmitVo.getId();
        super.saveOrUpdate(roomSubmitVo);

        if (id != null) {
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>()
                    .eq(GraphInfo::getItemType, ItemType.ROOM)
                    .eq(GraphInfo::getItemId, id));
            roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId, id));
            roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId,id));
            roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId,id));
            roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId,id));
            roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId,id));
        }

        ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
        roomSubmitVo.getGraphVoList().forEach(graphVo -> {
            GraphInfo graphInfo = new GraphInfo();
            BeanUtils.copyProperties(graphVo, graphInfo);
            graphInfo.setItemType(ItemType.ROOM);
            graphInfo.setItemId(roomSubmitVo.getId());
            graphInfoList.add(graphInfo);
        });
        graphInfoService.saveBatch(graphInfoList);

        ArrayList<RoomAttrValue> roomAttrValueList = new ArrayList<>();
        roomSubmitVo.getAttrValueIds().forEach(valueId -> {
            RoomAttrValue roomAttrValue = RoomAttrValue.builder().roomId(roomSubmitVo.getId()).attrValueId(valueId).build();
            roomAttrValueList.add(roomAttrValue);
        });
        roomAttrValueService.saveBatch(roomAttrValueList);

        ArrayList<RoomFacility> roomFacilityList = new ArrayList<>();
        roomSubmitVo.getFacilityInfoIds().forEach(facilityId -> {
            RoomFacility roomFacility = RoomFacility.builder().facilityId(facilityId).roomId(roomSubmitVo.getId()).build();
            roomFacilityList.add(roomFacility);
        });
        roomFacilityService.saveBatch(roomFacilityList);

        ArrayList<RoomLabel> roomLabelList = new ArrayList<>();
        roomSubmitVo.getLabelInfoIds().forEach(labelId -> {
            RoomLabel roomLabel = RoomLabel.builder().roomId(roomSubmitVo.getId()).labelId(labelId).build();
            roomLabelList.add(roomLabel);
        });
        roomLabelService.saveBatch(roomLabelList);

        ArrayList<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
        roomSubmitVo.getPaymentTypeIds().forEach(paymentTypeId->{
            RoomPaymentType roomPaymentType = RoomPaymentType.builder().roomId(roomSubmitVo.getId()).paymentTypeId(paymentTypeId).build();
            roomPaymentTypeList.add(roomPaymentType);
        });
        roomPaymentTypeService.saveBatch(roomPaymentTypeList);

        ArrayList<RoomLeaseTerm> roomLeaseTermList = new ArrayList<>();
        roomSubmitVo.getLeaseTermIds().forEach(leaseTermId -> {
            RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().roomId(roomSubmitVo.getId()).leaseTermId(leaseTermId).build();
            roomLeaseTermList.add(roomLeaseTerm);
        });
        roomLeaseTermService.saveBatch(roomLeaseTermList);

    }

}




